package kr.saeildamil.androidstudy_second.gesture;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.TextView;

public class GestureNavi extends Activity {
	TextView mResult;
	TextView mTextCount;
	
	int mCount = 10;
	GestureDetector mDetector;
	final static int DISTANCE = 150;
	final static int VELOCITY = 200;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gesturenavi);
		
		mResult = (TextView) findViewById(R.id.result);
		mTextCount = (TextView) findViewById(R.id.count);
		
		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setIsLongpressEnabled(false);
		mTextCount.setText(Integer.toString(mCount));
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	}
	
	OnGestureListener mGestureListener = new OnGestureListener() {
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(Math.abs(velocityX) > VELOCITY){//좌우로 fling했을때 기준 가속도
				if (e1.getX() - e2.getX() > DISTANCE || e1.getY() - e2.getY() > DISTANCE){
					//  <----
					mCount--;
				}
				if (e2.getX() - e1.getX() > DISTANCE || e2.getY() - e1.getY() > DISTANCE){
					//  ---->
					mCount++;
				}
				mTextCount.setText(Integer.toString(mCount));
				String text = String.format(
						"X축 거리 = %d, Y축 거리 = %d\n X축 속도 = %d, Y축 속도 = %d",
						(int)Math.abs(e1.getX()-e2.getX()), 
						(int)Math.abs(e1.getY()-e2.getY()), 
						(int)velocityX, (int)velocityY);
				mResult.setText(text);
			}
			return true;
		}
		
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}
		public void onShowPress(MotionEvent e) {}
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			return false;
		}
		public void onLongPress(MotionEvent e) {}
		public boolean onDown(MotionEvent e) {
			return false;
		}
	};
}
